package br.com.dev.tcc.view.sinistro;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import br.com.dev.tcc.R;
import br.com.dev.tcc.adapter.ViewPagerSinistroAdapter;
import br.com.dev.tcc.bean.Apolice;
import br.com.dev.tcc.bean.sinistro.Sinistro;
import br.com.dev.tcc.model.SinistroModel;
import br.com.dev.tcc.singleton.SinistroSingleton;
import br.com.dev.tcc.view.DefaultSupportActionBarActivity;
import br.com.dev.tcc.view.dialog.DecisionDialog;
import br.com.dev.tcc.view.sinistro.AcompanharSinistroFragment.DelegateSinistroTask;
import br.com.dev.tcc.view.sinistro.SelecionaApoliceFragment.OnApoliceSelected;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.SubMenu;
import com.actionbarsherlock.view.Window;

public class SinistroActivity extends DefaultSupportActionBarActivity {

	private SinistroModel model;
	private ActionBar mActionBar;
	private ViewPager mPager;
	private Tab tab;
	private SelecionaApoliceFragment fragmentCriar;
	private AcompanharSinistroFragment fragmentAcomp;
	private MenuItem menuItemSync;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(null);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.sinistro_activity);
		setActionBar(R.string.sinistro, R.drawable.sinistro);

		setSupportProgressBarIndeterminateVisibility(false);

		mActionBar = getSupportActionBar();
		mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		mPager = (ViewPager) findViewById(R.id.pager);

		model = new SinistroModel(this);

		FragmentManager fm = getSupportFragmentManager();

		ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				super.onPageSelected(position);
				mActionBar.setSelectedNavigationItem(position);
			}
		};

		mPager.setOnPageChangeListener(ViewPagerListener);
		ViewPagerSinistroAdapter viewpageradapter = new ViewPagerSinistroAdapter(
				fm, fragmentCriar = new SelecionaApoliceFragment(
						listenerListaApolices),
				fragmentAcomp = new AcompanharSinistroFragment(delegate));
		mPager.setAdapter(viewpageradapter);

		ActionBar.TabListener tabListener = new ActionBar.TabListener() {
			@Override
			public void onTabUnselected(Tab tab, FragmentTransaction ft) {
			}

			@Override
			public void onTabSelected(Tab tab, FragmentTransaction ft) {
				mPager.setCurrentItem(tab.getPosition());
			}

			@Override
			public void onTabReselected(Tab tab, FragmentTransaction ft) {
			}
		};

		tab = mActionBar.newTab().setText(R.string.novo)
				.setTabListener(tabListener);
		mActionBar.addTab(tab);

		tab = mActionBar.newTab().setText(R.string.acompanhar)
				.setTabListener(tabListener);
		mActionBar.addTab(tab);
	}

	@Override
	protected void onResume() {
		super.onResume();
		fragmentAcomp.buscaSinistros();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		SubMenu subMenu = menu.addSubMenu(null);
		menuItemSync = subMenu.getItem();
		menuItemSync.setIcon(android.R.drawable.ic_popup_sync);
		menuItemSync.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
		menuItemSync.setTitle(R.string.atualizar);

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		if (item.getTitle()
				.equals(getResources().getString(R.string.atualizar))) {

			if (fragmentAcomp.hasSinistro()) {
				mActionBar.setSelectedNavigationItem(1);
				showProgress();
				fragmentAcomp.atualiza();
			}
			else{
				Toast.makeText(this, R.string.erro_nao_ha_sinistros, 1).show();
			}
			return true;
		}

		return super.onOptionsItemSelected(item);
	}

	protected void onActivityResult(int arg0, int arg1,
			android.content.Intent arg2) {
		fragmentCriar.atualiza();
	};

	private void showProgress() {
		menuItemSync.setVisible(false);
		setSupportProgressBarIndeterminateVisibility(true);
	}

	private void hideProgress() {
		setSupportProgressBarIndeterminateVisibility(false);
		menuItemSync.setVisible(true);
	}

	private DelegateSinistroTask delegate = new DelegateSinistroTask() {

		@Override
		public void update(Sinistro sinistro) {
			model.atualiza(sinistro);
		}

		@Override
		public void onFinishUpdate() {
			hideProgress();
		}

		@Override
		public List<Sinistro> busca() {
			return model.lista();
		}

		@Override
		public void onDelete(final int idSinistro) {
			final DecisionDialog dialog = new DecisionDialog(SinistroActivity.this);
			dialog.setMessage(R.string.remover_sinistro);
			dialog.hideImage();
			dialog.setPositiveButtonListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					model.remove(idSinistro);
					fragmentAcomp.buscaSinistros();
					dialog.dismiss();
				}
			});
			dialog.setNegativeButtonListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					dialog.dismiss();
				}
			});
			dialog.show();
		}
	};

	private OnApoliceSelected listenerListaApolices = new OnApoliceSelected() {

		@Override
		public void onSelected(Apolice apolice) {
			SinistroSingleton.getInstance().setApolice(apolice);
			startActivity(new Intent(SinistroActivity.this,
					SelecionaTipoSinistroActivity.class));
		}
	};
}
